package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.dto.LoginDto;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private HttpServletRequest httpServletRequest;
    //  使用redis
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //  手机端验证码
    @PostMapping("/sendMsg")
    public R sendMsg(@RequestBody LoginDto loginDto) {

        // 使用随机数生成验证码
        Random random = new Random();
        int code = random.nextInt(100000) + 99999;
        System.out.println("验证码为:" + code);

//        stringRedisTemplate.opsForValue().set(loginDto.getPhone(), String.valueOf(code));
//        // 设置手机验证码的生命周期
//        stringRedisTemplate.expire(loginDto.getPhone(),5, TimeUnit.SECONDS);
        // 存值同时设定生命周期
        stringRedisTemplate.opsForValue().set(loginDto.getPhone(), String.valueOf(code), 5, TimeUnit.SECONDS);
        return R.success(code);
    }

    @Autowired
    private UserService userService;

    //  手机端客户登陆
    @PostMapping("/login")
    public R login(@RequestBody LoginDto loginDto) {
        if (loginDto == null) {
            return R.error("参数非法");
        }
        HttpSession session = httpServletRequest.getSession();
//        String getCode = (String) session.getAttribute(loginDto.getPhone());
        String getCode = stringRedisTemplate.opsForValue().get(loginDto.getPhone());
        if (getCode == null) {
            return R.error("验证码为空");
        }
        if (!getCode.equals(loginDto.getCode())) {
            return R.error("验证码错误");
        }
        //  登陆 判断是否是第一次登陆,如果是第一次登陆则需要将电话保存到数据库 user表内
        User user = userService.findByPhone(loginDto.getPhone());
        if (user == null) {
            // 说明是第一次登陆  需要将电话保存到数据库 user表内
            User addUser = userService.addPhone(loginDto.getPhone());
            session.setAttribute("userId", addUser.getId());
//            stringRedisTemplate.opsForValue().set(loginDto.getPhone(), String.valueOf(addUser.getId()));

            return R.success("登陆成功");
        }
        session.setAttribute("userId", user.getId());
//        stringRedisTemplate.opsForValue().set(loginDto.getPhone(), String.valueOf(user.getId()));
        return R.success("登陆成功");
    }

    //  登出
    @PostMapping("/loginout")
    public R loginout() {
        HttpSession session = httpServletRequest.getSession();
        session.removeAttribute("userId");
        return R.success("退出成功");
    }
}
